package hu.diploma.android.serviceimpl;

import hu.diploma.common.service.CallsService;
import hu.diploma.common.vos.CallVo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;
import android.provider.CallLog;
import android.util.Log;

public class CallsServiceImpl implements CallsService {
	private final ContentResolver contentResolver;

	public CallsServiceImpl(ContentResolver contentResolver) {
		super();
		this.contentResolver = contentResolver;
	}

	@Override
	public CallVo save(CallVo callVo) throws Exception {
		ContentValues values = new ContentValues();

		values.put(CallLog.Calls.CACHED_NAME, callVo.getName());
		values.put(CallLog.Calls.TYPE, callVo.getType());
		values.put(CallLog.Calls.NUMBER, callVo.getNumber());
		values.put(CallLog.Calls.DATE, callVo.getDate().getTime());
		values.put(CallLog.Calls.DURATION, callVo.getDuration());

		Uri uri = contentResolver.insert(CallLog.Calls.CONTENT_URI, values);
		long id = ContentUris.parseId(uri);
		callVo.setPhoneId(id);

		return callVo;
	}

	@Override
	public CallVo merge(CallVo vo) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public CallVo findById(Serializable searchId) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public CallVo findByPhoneId(Serializable searchId) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<CallVo> loadAll() throws Exception {
		Cursor c = contentResolver.query(CallLog.Calls.CONTENT_URI, null, null, null, null);

		ArrayList<CallVo> callsVos = new ArrayList<CallVo>();
		try {

			while (c.moveToNext()) {

				Integer id = c.getInt(c.getColumnIndex(BaseColumns._ID));
				String name = c.getString(c.getColumnIndexOrThrow(CallLog.Calls.CACHED_NAME));
				String type = c.getString(c.getColumnIndexOrThrow(CallLog.Calls.TYPE));
				String number = c.getString(c.getColumnIndexOrThrow(CallLog.Calls.NUMBER));
				String date = c.getString(c.getColumnIndexOrThrow(CallLog.Calls.DATE));
				String duration = c.getString(c.getColumnIndexOrThrow(CallLog.Calls.DURATION));

				CallVo callsVo = new CallVo();
				callsVo.setPhoneId(id.longValue());
				callsVo.setName(name);
				callsVo.setNumber(number);
				callsVo.setType(type);
				callsVo.setDate(new Date(Long.parseLong(date)));
				callsVo.setDuration(duration);

				callsVos.add(callsVo);
				Log.d("DEBUG", callsVo.toString());
			}
		} finally {
			c.close();
		}
		return callsVos;
	}

	@Override
	public void delete(CallVo vo) throws Exception {
		String where = BaseColumns._ID + " = ?";
		String[] whereParameters = new String[] { vo.getPhoneId().toString() };
		contentResolver.delete(CallLog.Calls.CONTENT_URI, where, whereParameters);
	}

}
